import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class 字典树的实现_哈希表 {
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    public static TreeNode root;
    public static int n;
    public static void main(String[] args) throws IOException {
        root = new TreeNode();
        n = in.nextInt();
        while (n-- > 0) {
            int q = in.nextInt();
            String word = in.next();
            if (q == 1) {
                insert(word);
            } else if (q == 2) {
                delete(word);
            } else if (q == 3) {
                out.println(countWordsEqualTo(word) > 0 ? "YES" : "NO");
            } else {
                out.println(countWordsStartingWith(word));
            }
        }
        out.close();
    }

    // 类实现字典树
    public static class TreeNode {
        int pass; // 该节点经过的节点数
        int end; // 该节点是不是到此为止, 也就是一个字符串
        Map<Integer, TreeNode> next; // 路径, 根据字母 'a' - 'z'
        // 进行初始化操作
        public TreeNode () {
            pass = 0;
            end = 0;
            next = new HashMap<>();
        }
    }

    // 对 word 进行一个插入操作
    public static void insert (String word) {
        TreeNode cur = root;
        cur.pass++;
        for (int i = 0; i < word.length(); i++) {
            int path = word.charAt(i);
            if (!cur.next.containsKey(path)) {
                cur.next.put(path,new TreeNode());
            }
            cur = cur.next.get(path);
            cur.pass++;
        }
        cur.end++;
    }

    public static void delete (String word) {
        if (countWordsEqualTo(word) > 0) {
            TreeNode cur = root;
            root.pass--;
            for (int i = 0; i < word.length(); i++) {
                int path = word.charAt(i);
                if (--cur.next.get(path).pass == 0) {
                    cur.next.remove(path);
                    return;
                }
                cur = cur.next.get(path);
            }
            cur.end--;
        }
    }

    // 查询前缀树里，word单词出现了几次
    private static int countWordsEqualTo(String word) {
        TreeNode cur = root;
        for (int i = 0; i < word.length(); i++) {
            int path = word.charAt(i);
            if (!cur.next.containsKey(path)) {
                return 0;
            }
            cur = cur.next.get(path);
        }
        return cur.end;
    }

    // 查询前缀树里，有多少单词以pre做前缀
    private static int countWordsStartingWith (String pre) {
        TreeNode cur = root;
        for (int i = 0; i < pre.length(); i++) {
            int path = pre.charAt(i);
            if (!cur.next.containsKey(path)) {
                return 0;
            }
            cur = cur.next.get(path);
        }
        return cur.pass;
    }


    public static class Read {
        StringTokenizer st = new StringTokenizer("");
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

        String next() throws IOException {
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(bf.readLine());
            }
            return st.nextToken();
        }

        int nextInt() throws IOException {
            return Integer.parseInt(next());
        }

        long nextLong() throws IOException {
            return Long.parseLong(next());
        }
    }
}













